<template>

   <!-- 表单组件 inline变成行内模式 -->
    <!-- 默认为true -->
   <el-form inline>
      <el-form-item>
         <!-- 选择器 -->
         <!-- 选难度 -->
         <selector v-model="params.difficulty" placeholder="请选择题目难度" style="width: 200px;"></selector>
      </el-form-item>
      <el-form-item>
         <!-- 选题目 -->
         <el-input v-model="params.title" placeholder="请您输入要搜索的题目标题" />
      </el-form-item>
      <el-form-item>
         <!-- 按钮 plain定义类型 -->
         <el-button plain @Click="onSearch">搜索</el-button>
         <el-button plain type="info" @click="onReset">重置</el-button>
         <!-- :icon添加图案 -->
         <el-button plain type="primary" :icon="Plus" @click="onAddQuestion">添加题目</el-button>
      </el-form-item>
   </el-form>


   <!-- 表格组件 与questionList数据关联 -->
   <el-table height="526px" :data="questionList">
      <!-- 每一列的定义 数据 宽度 列名 -->
      <el-table-column prop="questionId" width="180px" label="题目id" />
      <el-table-column prop="title" label="题目标题" />
      <el-table-column prop="difficulty" label="题目难度" width="90px">
         <template #default="{ row }">
            <!-- 默认插槽 v-if语句表选择 "row"代表了当前行的数据。 -->
            <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
            <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
            <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
         </template>
      </el-table-column>
      <el-table-column prop="createName" label="创建人" width="140px" />
      <el-table-column prop="createTime" label="创建时间" width="180px" />
      <el-table-column label="操作" width="100px" fixed="right">
         <template #default="{ row }">
            <!-- 编辑 -->
            <el-button type="text" @click="onEdit(row.questionId)">编辑
            </el-button>
            <!-- 删除 -->
            <el-button type="text" class="red" @click="onDelete(row.questionId)">删除
            </el-button>
         </template>
      </el-table-column>
   </el-table>


   <!-- //分页 -->
   <!-- //v-model:current-page="params.pageNum": 使用Vue的v-model绑定当前页码到params.pageNum,实现了数据双向绑定。 -->
   <el-pagination background size="small" layout="total, sizes, prev, pager, next, jumper" :total="total"
      v-model:current-page="params.pageNum" v-model:page-size="params.pageSize" :page-sizes="[1, 5, 10, 15, 20]"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />

   <!-- 抽屉通过questionEditRef调用question-drawer组件中的方法 -->
   <!-- @success="onSuccess" 是一个事件监听器，它监听 <question-drawer> 组件触发的 success 自定义事件。
         当 success 事件被触发时，它会调用当前组件的 onSuccess 方法。 -->
   <question-drawer ref="questionEditRef" @success="onSuccess"></question-drawer>
</template>

<script setup>
import { Plus } from "@element-plus/icons-vue"
import Selector from "@/components/QuestionSelector.vue"
import { getQuestionListService, delQuestionService } from "@/apis/question.js"
import { reactive, ref } from "vue";
import QuestionDrawer from "@/components/QuestionDrawer.vue"

//reactive定义响应式数据，与上图数据相绑定
const params = reactive({
   pageNum: 1,
   pageSize: 10,
   difficulty: '',
   title: ''
})

const questionList = ref([])
//创建一个响应式的引用对象
const total = ref(0)

async function getQuestionList() {
   const result = await getQuestionListService(params)
   console.log(result)
   questionList.value = result.rows
   total.value = result.total
}
getQuestionList()

//每页数量
function handleSizeChange(newSize) {
   // params.pageSize = newSize
   params.pageNum = 1
   getQuestionList()
}

//换页码
function handleCurrentChange(newPage) {
   // params.pageNum = newPage
   getQuestionList()
}

//搜索
function onSearch() {
   params.pageNum = 1
   getQuestionList()
}
//重置
function onReset() {
   params.pageNum = 1
   params.pageSize = 10
   params.title = ''
   params.difficulty = ''
   getQuestionList()
}

const questionEditRef = ref()
// 添加
const onAddQuestion = () => {
   //调用组件方法
   questionEditRef.value.open()
}

// 添加 和 编辑
function onSuccess(service) {
   if (service === 'add') {
      params.pageNum = 1
   }
   getQuestionList()
}

//编辑
async function onEdit(questionId) {
   questionEditRef.value.open(questionId)
}

//删除
async function onDelete(questionId) {
   await delQuestionService(questionId)
   params.pageNum = 1
   getQuestionList()
}
</script>